package com.quanyan.place.service.impl;

import com.quanyan.api.APIResponse;
import com.quanyan.common.constants.Constants;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.TbBizPlaceAttachLabel;
import com.quanyan.place.entity.TbBizPlaceAttachLabelExample;
import com.quanyan.place.entity.TbLabel;
import com.quanyan.place.entity.TbLabelExample;
import com.quanyan.place.entity.vo.PlaceLabelVo;
import com.quanyan.place.entity.vo.ReqPlaceLabel;
import com.quanyan.place.mapper.TbBizPlaceAttachLabelMapper;
import com.quanyan.place.mapper.TbLabelMapper;
import com.quanyan.place.service.PlaceLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangyun on 2017/5/25.
 */
@Service("placeLabelService")
public class PlaceLabelServiceImpl implements PlaceLabelService {

    @Autowired
    TbBizPlaceAttachLabelMapper tbBizPlaceAttachLabelMapper;

    @Autowired
    TbLabelMapper tbLabelMapper;

    /**
     * 通过标签类型查询标签
     * @param labelType
     * @return
     */
    @Override
    public List<TbLabel> getLabelByType(Byte labelType) {
        TbLabelExample example = new TbLabelExample();
        TbLabelExample.Criteria criteria = example.createCriteria();
        if (null != labelType){
            criteria.andTypeEqualTo(labelType);
        }
        List<TbLabel> tbLabelList = tbLabelMapper.selectByExample(example);
        return tbLabelList;
    }

    /**
     * 通过场馆标签Id查询标签
     * @param id
     * @return
     */
    public TbBizPlaceAttachLabel getTbBizPlaceAttachLabel(Integer id) {
        return tbBizPlaceAttachLabelMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询场馆标签
     * @param placeId
     * @return
     */
    @Override
    public List<TbBizPlaceAttachLabel> getPlaceLabel(Integer placeId, Byte labelType) {
        TbBizPlaceAttachLabelExample example = new TbBizPlaceAttachLabelExample();
        TbBizPlaceAttachLabelExample.Criteria criteria = example.createCriteria();
        criteria.andPlaceIdEqualTo(placeId);
        if (null != labelType){
            criteria.andLabelTypeEqualTo(labelType);
        }
        List<TbBizPlaceAttachLabel> tbBizPlaceAttachLabels = tbBizPlaceAttachLabelMapper.selectByExample(example);
        return tbBizPlaceAttachLabels;
    }

    public List<PlaceLabelVo> getPlaceLabelVo(Integer placeId, Byte labelType) {
        List<PlaceLabelVo> placeLabelVoList = new ArrayList<PlaceLabelVo>();
        List<TbBizPlaceAttachLabel> tbBizPlaceAttachLabels = this.getPlaceLabel(placeId, labelType);
        for (TbBizPlaceAttachLabel label : tbBizPlaceAttachLabels){
            TbLabel tbLabel = this.getTbLabelById(label.getLabelId());
            PlaceLabelVo labelVo = new PlaceLabelVo();
            labelVo.setId(label.getId());
            labelVo.setPlaceId(label.getPlaceId());
            labelVo.setDisplayName(label.getDisplayName());
            labelVo.setLabelInfo(label.getLabelInfo());
            labelVo.setLabelType(label.getLabelType());
            if (null != tbLabel && com.alibaba.dubbo.common.utils.StringUtils.isNotEmpty(tbLabel.getIcon())){
                labelVo.setIconUrl(tbLabel.getIcon());
            }
            placeLabelVoList.add(labelVo);
        }
        return placeLabelVoList;
    }

    public TbLabel getTbLabelById(Integer labelId) {
        return tbLabelMapper.selectByPrimaryKey(labelId);
    }

    /**
     * 设置场馆标签
     * @param reqPlaceLabel
     * @return
     */
    @Override
    public APIResponse saveOrUpdatePlaceLabel(ReqPlaceLabel reqPlaceLabel) {
        if (null == reqPlaceLabel){
            return APIResponse.returnFail("标签信息不能为空");
        }
        if (null == reqPlaceLabel.getPlaceId()){
            return APIResponse.returnFail("场馆Id不能为空");
        }
        Integer placeId = reqPlaceLabel.getPlaceId();
        for (PlaceLabelVo labelVo : reqPlaceLabel.getPlaceLabelVoList()){
            TbBizPlaceAttachLabel tbBizPlaceAttachLabel = new TbBizPlaceAttachLabel();
            if (null != labelVo.getId()){
                tbBizPlaceAttachLabel = this.getTbBizPlaceAttachLabel(labelVo.getId());
            }
            tbBizPlaceAttachLabel.setPlaceId(placeId);
            tbBizPlaceAttachLabel.setLabelType(labelVo.getLabelType()); // 标签类型
            tbBizPlaceAttachLabel.setLabelId(labelVo.getLabelId()); // 标签Id
            tbBizPlaceAttachLabel.setLabelInfo(labelVo.getLabelInfo());
            tbBizPlaceAttachLabel.setDisplayName(labelVo.getDisplayName());
            //tbBizPlaceAttachLabel.setIsDelete(Constants.IS_DELETE_FALSE);
            if (null == labelVo.getId()){
                tbBizPlaceAttachLabelMapper.insertSelective(tbBizPlaceAttachLabel);
            } else {
                tbBizPlaceAttachLabelMapper.updateByPrimaryKey(tbBizPlaceAttachLabel);
            }
        }
        return APIResponse.returnSuccess("设置成功");
    }

    /**
     * 获取场馆标签配置
     * @return
     */
    @Override
    public Map<Byte,List<TbLabel>> getPlaceCommonLabel() {
        List<TbLabel> labelList = this.getLabelByType(null);
        Map<Byte,List<TbLabel>> labelMap = new HashMap<Byte,List<TbLabel>>();
        for (TbLabel label : labelList){
            if (PlaceConstants.LABEL_TYPE_ONE.equals(label.getType()) ||
                    PlaceConstants.LABEL_TYPE_TWO.equals(label.getType()) ||
                    PlaceConstants.LABEL_TYPE_THREE.equals(label.getType())){
                List<TbLabel> result = null;
                if (labelMap.containsKey(label.getType())){
                    result = labelMap.get(label.getType());
                } else {
                    result = new ArrayList<TbLabel>();
                }
                result.add(label);
                labelMap.put(label.getType(),result);
            }
        }
        return labelMap;
    }


    /**
     * 获取场馆标签
     * @param placeId
     * @return
     */
    @Override
    public Map<Byte,List<TbBizPlaceAttachLabel>> getPlaceLabelByPlaceId(Integer placeId) {
        Map<Byte,List<TbBizPlaceAttachLabel>> labelMap = new HashMap<Byte,List<TbBizPlaceAttachLabel>>();
        List<TbBizPlaceAttachLabel> tbBizPlaceAttachLabels = this.getPlaceLabel(placeId, null);
        // 按标签类型分组封装到map中
        for (TbBizPlaceAttachLabel attachLabel: tbBizPlaceAttachLabels){
            List<TbBizPlaceAttachLabel> result = null;
            if (labelMap.containsKey(attachLabel.getLabelType())){
                result = labelMap.get(attachLabel.getLabelType());
            } else {
                result = new ArrayList<TbBizPlaceAttachLabel>();
            }
            result.add(attachLabel);
            labelMap.put(attachLabel.getLabelType(),result);

        }
        return labelMap;
    }
}
